﻿using CsvHelper;
using Inovout.Kuajing.Models;
using Inovout.Kuajing.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Kuajing.Store.Services
{
    public class CVSService
    {
        private ShipmentOrderItemService shipmentOrderItemService;
        private ShipmentOrderService shipmentOrderService;
        public CVSService()
        {
            shipmentOrderItemService = ComponentRegistry.Resolve<ShipmentOrderItemService>();
            shipmentOrderService = ComponentRegistry.Resolve<ShipmentOrderService>();
        }

        public void WriteCsvFile(string[] properties, string filePath, string consignerCode, bool tab, string templateHeader)
        {
            Stream fs = System.IO.File.Open(filePath, FileMode.Create);
            TextWriter fileWriter = new StreamWriter(fs, Encoding.UTF8);
            var csv = new CsvWriter(fileWriter);

            IList<ShipmentOrderItem> listShipmentOrderItem = shipmentOrderItemService.GetByLogisticsCodeAndStatus(consignerCode).OrderByDescending(model => model.CreatedTime).ToList();

            string[] headers = templateHeader.Split(',');
            for (int i = 0; i < headers.Length; i++)
            {
                csv.WriteField(headers[i]);
            }
            csv.NextRecord();

            foreach (ShipmentOrderItem shipmentOrderItem in listShipmentOrderItem)
            {
                for (int i = 0; i < properties.Length; i++)
                {
                    string[] ps = properties[i].Split('.');
                    if (ps[0] == "ShipmentOrder")
                    {
                        Object obj = shipmentOrderItem.ShipmentOrder.GetValue(ps[1]);
                        if (obj != null)
                        {
                            if (tab)
                            {
                                csv.WriteField("\t" + obj);
                            }
                            else
                            {
                                csv.WriteField(obj);
                            }

                        }
                        else
                        {
                            csv.WriteField(string.Empty);
                        }

                    }

                    if (ps[0] == "ShipmentOrderItem")
                    {
                        Object obj = shipmentOrderItem.GetValue(ps[1]);
                        if (obj != null)
                        {
                            if (tab)
                            {
                                csv.WriteField("\t" + obj);
                            }
                            else
                            {
                                csv.WriteField(obj);
                            }
                        }
                        else
                        {
                            csv.WriteField(string.Empty);
                        }
                    }

                    if (ps[0] == "Commodity")
                    {
                        Object obj = shipmentOrderItem.Commodity.GetValue(ps[1]);
                        if (obj != null)
                        {
                            if (tab)
                            {
                                csv.WriteField("\t" + obj);
                            }
                            else
                            {
                                csv.WriteField(obj);
                            }
                        }
                        else
                        {
                            csv.WriteField(string.Empty);
                        }
                    }

                    if (ps[0] == "Const")
                    {
                        if (tab)
                        {

                            csv.WriteField("\t" + ps[1]);
                        }
                        else
                        {
                            csv.WriteField(ps[1]);
                        }

                    }
                }
                csv.NextRecord();
            }
            fileWriter.Close();
            fs.Close();
        }


        public IList<ShipmentOrder> ReadCsvFile(string filePath, string consignerCode)
        {
            Stream fs = System.IO.File.Open(filePath, FileMode.Open);
            TextReader fileReader = new StreamReader(fs);
            var csv = new CsvReader(fileReader);
            IList<ShipmentOrder> listShipmentOrder = new List<ShipmentOrder>();
            while (csv.Read())
            {
                ShipmentOrder shipmentOrder = new ShipmentOrder();
                object id = csv.GetField("订单号");
                if (id != null)
                {
                    shipmentOrder = shipmentOrderService.GetByNo(id.ToString());
                }

                object logisticsOrderNo = csv.GetField("运单号");
                if (logisticsOrderNo != null)
                {
                    shipmentOrder.LogisticsOrderNo = logisticsOrderNo.ToString();
                }

                object logisticsCompanyName = csv.GetField("物流公司");
                if (logisticsCompanyName != null)
                {
                    shipmentOrder.LogisticsCompanyName = logisticsCompanyName.ToString();
                }

                object logisticsCompanyCode = csv.GetField("物流公司代码");
                if (logisticsCompanyCode != null)
                {
                    shipmentOrder.LogisticsCompanyCode = logisticsCompanyCode.ToString();
                }
                listShipmentOrder.Add(shipmentOrder);

            }
            fileReader.Close();
            fs.Close();

            return listShipmentOrder.Where(n => n.ExternalShipperId == consignerCode).ToList();
            //return listShipmentOrder.ToList();
        }
    }
}
